package org.eden.heartbeat.api.configuration;

import org.eden.heartbeat.api.exception.ConfigurationException;
import org.eden.heartbeat.api.exception.ConfigurationNotFoundException;
import org.eden.heartbeat.api.utils.HeartbeatStringUtils;

import java.io.IOException;
import java.util.Properties;

/**
 * Class that holds the application configurations
 *
 * @author trsimoes
 * @version $Revision:$
 */
public class ApplicationConfiguration {

    /**
     * The JMX service port.
     */
    public static int jmxPort;

    /**
     * Method that initializes the application configurations.
     *
     * @throws ConfigurationException thrown when an error occurs while loading the configurations.
     */
    public static void loadConfigurations() throws ConfigurationException, ConfigurationNotFoundException {
        readFromFile();
    }

    private static void readFromFile() throws ConfigurationException, ConfigurationNotFoundException {

        Properties properties = new Properties();

        try {
            properties.load(ApplicationConfiguration.class.getResourceAsStream("/heartbeat.properties"));
        } catch (IOException e) {
            throw new ConfigurationException(e);
        }

        String jmxPortString = properties.getProperty("jmx.port");
        if (!HeartbeatStringUtils.isNullOrEmpty(jmxPortString)) {
            try {
                jmxPort = Integer.valueOf(jmxPortString);
            } catch (NumberFormatException e) {
                throw new ConfigurationNotFoundException("jmx.port");
            }
        } else {
            throw new ConfigurationNotFoundException("jmx.port");
        }
    }

}